/**
 * Copyright (c) 2024 - CARThunder
 * All Rights Reserved by Chengdu CARThunder Intelligent Technology Co., LTD and its affiliates.
 * You may not use, copy, distribute, modify, transmit in any form this file
 * except in compliance with CARThunder in writing by applicable law.
 * <p>
 * <p>
 * file description.
 *
 * @file filename
 * @brief brief function description.
 * @details detailed function description.
 * @version 1.0
 * @author author_name
 * @date last_modified_date
 * <p>
 * Edit History
 * ----------------------------------------------------------------------------
 * DATE                     NAME               DESCRIPTION
 * 2024-08-2               jia fengyi         Create it.
 */

/**
 * file description.
 * @file filename
 * @brief brief function description.
 * @details detailed function description.
 * @version 1.0
 * @author author_name
 * @date last_modified_date
 *
 *      Edit History
 *      ----------------------------------------------------------------------------
 *      DATE                     NAME               DESCRIPTION
 *      2024-08-2               jia fengyi         Create it.
 *
 */

package com.cart.musicdemo.fragment;

import android.os.Bundle;
import android.os.Parcelable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.cart.musicdemo.MainActivity;
import com.cart.musicdemo.R;
import com.cart.musicdemo.adapter.PlayListAdapter;
import com.cart.musicdemo.adapter.SongAdapter;
import com.cart.musicdemo.base.service.MusicService;
import com.cart.musicdemo.model.SongModel;
import com.cart.musicdemo.model.UpdateEventUI;
import com.umeng.commonsdk.debug.E;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

/**
 * The type Song fragment.
 */
public class SongFragment extends Fragment {

    private RecyclerView recyclerView;

    private MusicService.MusicBinder musicService;

    private List<SongModel> songList;


    private SongAdapter songAdapter;

    /**
     * Instantiates a new Song fragment.
     */
    public SongFragment() {
        // Required empty public constructor
    }

    /**
     * New instance song fragment.
     *
     * @param songs        the songs
     * @param musicService the music service
     * @return the song fragment
     */
    public static SongFragment newInstance(List<SongModel> songs,
                                           MusicService.MusicBinder musicService) {
        SongFragment fragment = new SongFragment();
        Bundle args = new Bundle();
        //传参
        args.putBinder("musicService", musicService);
        args.putParcelableArrayList("songs", (ArrayList<? extends Parcelable>) songs);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        EventBus.getDefault().register(this);
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            songList = getArguments().getParcelableArrayList("songs");
            musicService = (MusicService.MusicBinder) getArguments().getBinder("musicService");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onUpdateEventUI(UpdateEventUI event) {
        String type = event.getType();
        if (type.equals("clearUI")) {
            //拔出USB时清空数据
            songList.clear();
        }
        // 更新 UI
        songAdapter.notifyDataSetChanged();

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_song_list, container, false);
        recyclerView = view.findViewById(R.id.rv_song_RecyclerView);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        songAdapter = new SongAdapter(songList, this::onItemClick);
        recyclerView.setAdapter(songAdapter);
        return view;
    }

    private void onItemClick(int position, boolean reset) {
        // 更新音乐服务中的播放列表
        musicService.updateSongList(songList);
        // 设置当前播放位置
        musicService.setCurrentPosition(position);
        // 播放指定位置的歌曲，并指定是否自动播放下一首
        musicService.play(position, true);
        // 获取当前的 Activity 实例并更新其播放按钮的 UI
        MainActivity activity = (MainActivity) getActivity();
        if (activity != null) {
            // 更新 MainActivity 中播放按钮的状态
            activity.updateStartButtonStatus();
        }
        // 通知适配器数据已改变，更新 Fragment 中的 UI
        songAdapter.notifyDataSetChanged();


    }
}

